﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Runtime.InteropServices;
using System.Net.NetworkInformation;

namespace reportsDesktop
{
    class NetConnectState
    {
        //InternetGetConnectedState返回的状态标识位的含义：
        //private const int INTERNET_CONNECTION_MODEM = 1;
        //private const int INTERNET_CONNECTION_LAN = 2;
        //private const int INTERNET_CONNECTION_PROXY = 4;
        //private const int INTERNET_CONNECTION_MODEM_BUSY = 8;



        //[DllImport("wininet.dll")]
        //private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);

        //public string GetConnectState()
        //{
        //    int dwFlag = 0;
        //    string netstatus = "";
        //    if (!InternetGetConnectedState(out dwFlag, 0))
        //        netstatus += " 未联网 /n";
        //    if ((dwFlag & INTERNET_CONNECTION_MODEM) != 0)
        //        netstatus += " 采用调治解调器上网 /n";
        //    if ((dwFlag & INTERNET_CONNECTION_LAN) != 0)
        //        netstatus += " 采用网卡上网  /n";
        //    if ((dwFlag & INTERNET_CONNECTION_PROXY) != 0)
        //        netstatus += " 采用代理上网  /n";
        //    if ((dwFlag & INTERNET_CONNECTION_MODEM_BUSY) != 0)
        //        netstatus += " MODEM被其他非INTERNET连接占用  /n";
        //    return netstatus;
        //}

        //public bool ConnectTest2()
        //{
        //    if (GetConnectState().Contains("未联网"))
        //        return false;
        //    return true;
        //}
        public bool ConnectTest()
        {
            Ping p = new Ping();//创建Ping对象p
            try
            {
                PingReply pr = p.Send("www.baidu.com");//向指定IP或者主机名的计算机发送ICMP协议的ping数据包,百度IP：220.181.111.85
                if (pr.Status == IPStatus.Success)//如果ping成功
                    return true;
                else
                    return false;
            }
            catch (Exception e)
            {
                return false;
            }

        }


    }
}
